(ns web.app
  (:use home
        [util file]))

(def ^:dynamic *web-root* (file-join (mekit-home) "src/web"))

(defn web-app-home [app]
  (file-join *web-root* "app" app))

(defn web-app-resource [app path]
  (file-join (web-app-home app) path))

(defn web-app-file [app path]
  (java.io.File. (web-app-resource app path)))

(defn- mime-type [path]
  (cond
    (re-find #"(?i)\.css$" path) "text/css"
    (re-find #"(?i)\.js" path) "text/javascript"))

(defn web-app-file-with-mime-type [app path]
  (let [path (web-app-resource app path)
        body (java.io.File. path)]
    (if-let [mime-type (mime-type path)]
      {:body body
       :headers {"Content-Type" mime-type}}
      body)))

(def ^:dynamic *app-cookie*)

